package com.loan.mappers;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.loan.entity.Customer;
import com.loan.entity.User;
import com.loan.util.AUtils;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface CustomerMapper extends BaseMapper<Customer> {
    @ResultMap("baseMap")
   @Select("SELECT * FROM user u LEFT JOIN customer c ON u.id=c.user_id WHERE c.user_id IS NULL")
    List<Customer> listNoEmp();


    @Select("SELECT * FROM user u,customer c WHERE u.`id`=c.`user_id` and emp_id=#{empId} and level=#{level}")
    @Results(id = "baseMap",value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "empId", column = "emp_id"),
            @Result(property = "emp",
                    column = "emp_id",
                    one = @One(select = "com.loan.mappers.EmpMapper.selectById")),
            @Result(property = "level",column = "level"),
            @Result(property = "del",column = "del"),
            @Result(property = "userId",column = "user_id"),
            @Result(property = "user",
                    column = "user_id",
                    one = @One(select = "com.loan.mappers.UserMapper.selectById"))
    })
    List<Customer> listByLevel(String level, int empId);

    @ResultMap("baseMap")
    @Select("SELECT * FROM customer where user_id=#{userId}")
    Customer findByUserId(int userId);

    /**
     * 条件查询
     * @param customer
     * @return
     */
    @SelectProvider(type = SqlProvider.class,method = "select")
    @ResultMap("baseMap")
    List<Customer> findByCondition(Customer customer);

    /**
     * 根据cusId查所有信息
     * @return
     */
    @Select("select * from customer where id=#{id}")
    @ResultMap("baseMap")
    Customer findAllInfoById(Integer id);

    @UpdateProvider(type = SqlProvider.class, method = "update")
    void update(Customer customer);

    public static class SqlProvider {
        public static String update(Customer customer) {
            SQL sql = new SQL();
            sql.UPDATE("customer");
            if (AUtils.isNotEmpty(customer.getLevel())) {
                sql.SET("level=#{level}");
            }
            if (customer.getEmpId()!=null && customer.getEmpId()!=0){
                sql.SET("emp_id=#{empId}");
            }
            if (customer.getUserId()!=null && customer.getUserId()!=0){
                sql.SET("user_id=#{userId}");
            }
            if (customer.getDel()!=null){
                sql.SET("del=#{del}");
            }
            sql.WHERE("id=#{id}");
            return sql.toString();
        }

        public static String select(Customer customer) {
            SQL sql = new SQL();
            sql.SELECT("*").FROM("customer c,user u");
            sql.WHERE("c.user_id=u.id");
            if (customer == null){
                return sql.toString();
            }
            if (customer.getId() != null && customer.getId() != 0){
                sql.WHERE("id=#{id}");
            }
            if (customer.getEmpId() != null){
                sql.WHERE("emp_id=#{empId}");
            }
            if (AUtils.isNotEmpty(customer.getLevel())){
                sql.WHERE("level=#{level}");
            }
            sql.WHERE("del='n'");
            if (customer.getUserId()!=null){
                sql.WHERE("user_id=#{userId}");
            }
            if (customer.getUser()!=null){
                if (AUtils.isNotEmpty(customer.getUser().getName())){
                    sql.WHERE("u.name like concat('%',#{user.name},'%')");
                }
            }
            return sql.toString();
        }

    }

}
